package com.mdinic.blogovi.front.common;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;

import com.mdinic.blogovi.front.page.BSession;

public abstract class AjaxLinkPerson extends AjaxLink<Void> {

    private static final long serialVersionUID = -2883477565777353628L;

    private boolean refreshPage = true;

    public AjaxLinkPerson(String id, boolean refreshPage) {
        super(id);
        this.refreshPage = refreshPage;
    }

    public AjaxLinkPerson(String id) {
        super(id);
    }

    @Override
    public final void onClick(AjaxRequestTarget target) {
        if (BSession.get().isLoggedIn()) {
            click(target);
        } else {
            final AbstractBasePage page = (AbstractBasePage) getPage();
            page.showLogin(target, new IReturn() {

                private static final long serialVersionUID = 1L;

                @Override
                public void onReturn(AjaxRequestTarget target) {
                    click(target);
                    if (refreshPage) {
                        setResponsePage(getPage().getClass(), getPage().getPageParameters());
                    }
                }
            });
        }
    }

    public abstract void click(AjaxRequestTarget target);

}
